home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / os-prober / common.sh
Text File  |  2008-08-25  |  4KB  |  196 lines

  1. require_tmpdir() {
  2.   if [ -z "$OS_PROBER_TMP" ]; then
  3.     if type mktemp >/dev/null 2>&1; then
  4.       export OS_PROBER_TMP="$(mktemp -d /tmp/os-prober.XXXXXX)"
  5.       trap "rm -rf $OS_PROBER_TMP" EXIT HUP INT QUIT TERM
  6.     else
  7.       export OS_PROBER_TMP=/tmp
  8.     fi
  9.   fi
  10. }
  11.  
  12. count_for() {
  13.   _labelprefix=$1
  14.   _result=$(grep "^${_labelprefix} " /var/lib/os-prober/labels 2>/dev/null || true)
  15.  
  16.   if [ -z "$_result" ]; then
  17.     return
  18.   else
  19.     echo "$_result" | cut -d' ' -f2
  20.   fi
  21. }
  22.  
  23. count_next_label() {
  24.   require_tmpdir
  25.  
  26.   _labelprefix=$1
  27.   _cfor="$(count_for ${_labelprefix})"
  28.  
  29.   if [ -z "$_cfor" ]; then
  30.     echo "${_labelprefix} 1" >> /var/lib/os-prober/labels
  31.   else
  32.     sed "s/^${_labelprefix} ${_cfor}/${_labelprefix} $(($_cfor + 1))/" /var/lib/os-prober/labels > "$OS_PROBER_TMP/os-prober.tmp"
  33.     mv "$OS_PROBER_TMP/os-prober.tmp" /var/lib/os-prober/labels
  34.   fi
  35.   
  36.   echo "${_labelprefix}${_cfor}"
  37. }
  38.  
  39. log() {
  40.   logger -t "$(basename $0)" "$@"
  41. }
  42.  
  43. error() {
  44.   log "error: $@"
  45. }
  46.  
  47. warn() {
  48.   log "warning: $@"
  49. }
  50.  
  51. debug() {
  52.   log "debug: $@"
  53. }
  54.  
  55. result () {
  56.   log "result:" "$@"
  57.   echo "$@"
  58. }
  59.  
  60. # shim to make it easier to use os-prober outside d-i
  61. if ! type mapdevfs >/dev/null 2>&1; then
  62.   mapdevfs () {
  63.     readlink -f "$1"
  64.   }
  65. fi
  66.  
  67. item_in_dir () {
  68.     if [ "$1" = "-q" ]; then
  69.         q="-q"
  70.         shift 1
  71.     else
  72.         q=""
  73.     fi
  74.     # find files with any case
  75.     ls -1 "$2" | grep $q -i "^$1$"
  76. }
  77.  
  78. parse_proc_mounts () {
  79.     while read line; do
  80.         set -- $line
  81.         echo "$(mapdevfs $1) $2 $3"
  82.     done
  83. }
  84.  
  85. parsefstab () {
  86.     while read line; do
  87.         case "$line" in
  88.             "#"*)
  89.                 :    
  90.             ;;
  91.             *)
  92.                 set -- $line
  93.                 echo $1 $2 $3
  94.             ;;
  95.         esac
  96.     done
  97. }
  98.  
  99. linux_mount_boot () {
  100.     partition="$1"
  101.     tmpmnt="$2"
  102.  
  103.     bootpart=""
  104.     mounted=""
  105.     if [ -e "$tmpmnt/etc/fstab" ]; then
  106.         # Try to mount any /boot partition.
  107.         bootmnt=$(parsefstab < $tmpmnt/etc/fstab | grep " /boot ") || true
  108.         if [ -n "$bootmnt" ]; then
  109.             set -- $bootmnt
  110.             boottomnt=""
  111.             # This is an awful hack and isn't guaranteed to
  112.             # work, but is the best we can do until busybox
  113.             # mount supports -L/-U.
  114.             if [ -x "$tmpmnt/bin/mount" ]; then
  115.                 smart_ldlp="$tmpmnt/lib"
  116.                 smart_mount="$tmpmnt/bin/mount"
  117.             elif [ -x /target/bin/mount ]; then
  118.                 smart_ldlp=/target/lib
  119.                 smart_mount=/target/bin/mount
  120.             else
  121.                 smart_ldlp=
  122.                 smart_mount=mount
  123.             fi
  124.             if [ -e "$1" ]; then
  125.                 bootpart="$1"
  126.                 boottomnt="$1"
  127.             elif [ -e "$tmpmnt/$1" ]; then
  128.                 bootpart="$1"
  129.                 boottomnt="$tmpmnt/$1"
  130.             elif [ -e "/target/$1" ]; then
  131.                 bootpart="$1"
  132.                 boottomnt="/target/$1"
  133.             elif echo "$1" | grep -q "LABEL="; then
  134.                 debug "mounting boot partition by label for linux system on $partition: $1"
  135.                 label=$(echo "$1" | cut -d = -f 2)
  136.                 if LD_LIBRARY_PATH=$smart_ldlp $smart_mount -L "$label" -o ro $tmpmnt/boot -t "$3"; then
  137.                     mounted=1
  138.                     bootpart=$(mount | grep $tmpmnt/boot | cut -d " " -f 1)
  139.                 else
  140.                     error "failed to mount by label"
  141.                 fi
  142.             elif echo "$1" | grep -q "UUID="; then
  143.                 debug "mounting boot partition by UUID for linux system on $partition: $1"
  144.                 uuid=$(echo "$1" | cut -d = -f 2)
  145.                 if LD_LIBRARY_PATH=$smart_ldlp $smart_mount -U "$uuid" -o ro $tmpmnt/boot -t "$3"; then
  146.                     mounted=1
  147.                     bootpart=$(mount | grep $tmpmnt/boot | cut -d " " -f 1)
  148.                 else
  149.                     error "failed to mount by UUID"
  150.                 fi
  151.             else
  152.                 bootpart=""
  153.             fi
  154.  
  155.             if [ ! "$mounted" ]; then
  156.                 if [ -z "$bootpart" ]; then
  157.                     debug "found boot partition $1 for linux system on $partition, but cannot map to existing device"
  158.                 else
  159.                     debug "found boot partition $bootpart for linux system on $partition"
  160.                     if mount -o ro "$boottomnt" $tmpmnt/boot -t "$3"; then
  161.                         mounted=1
  162.                     else
  163.                         error "failed to mount $boottomnt on $tmpmnt/boot"
  164.                     fi
  165.                 fi
  166.             fi
  167.         fi
  168.     fi
  169.     if [ -z "$bootpart" ]; then
  170.         bootpart="$partition"
  171.     fi
  172.     if [ -z "$mounted" ]; then
  173.         mounted=0
  174.     fi
  175.  
  176.     echo "$bootpart $mounted"
  177. }
  178.  
  179. # Unmount a file system. If this fails, wait for a bit and try again, to
  180. # cope with crazy automounting systems opportunistically opening it.
  181. repeat_umount() {
  182.     local i=3
  183.     while [ "$i" != 0 ]; do
  184.         if umount "$1"; then
  185.             break
  186.         fi
  187.         i="$(($i - 1))"
  188.         if [ "$i" = 0 ]; then
  189.             warn "failed to unmount $1; things may go wrong"
  190.         else
  191.             warn "failed to unmount $1; trying again ($i)"
  192.             sleep 5
  193.         fi
  194.     done
  195. }
  196.